package com.swp.oa.sys.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swp.oa.sys.dto.domian.UserDto;
import com.swp.oa.sys.dto.page.JsonMessage;
import com.swp.oa.sys.service.IAuditService;
import com.swp.oa.sys.service.IUserService;
@Controller
@RequestMapping("/account")
public class AccountController extends BaseController{

	@Resource
	private IUserService userService;
	@Resource
	private IAuditService auditService;
	
	// 转到登录页面
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String toLogin() {
		return "/Account/Login";
	}
	//返回修改密码页面
	@RequestMapping(value="/toChangePasswordByToken",method=RequestMethod.GET)
	public String toChangePasswordByToken(String token, HttpServletRequest request)
	{
		System.out.println("come in toChangePasswordByToken -token: " + token);
		String userId = userService.getUserIdByToken(token);
		System.out.println("userId: " + userId);
		System.out.println();
		request.setAttribute("userId", userId);
		return "/Account/ChangePasswordByToken";
	}
	//返回修改密码页面
	@RequestMapping(value="/toChangePassword",method=RequestMethod.GET)
	public String toChangePassword(HttpServletRequest request)
	{
		System.out.println("come in toChangePassword -");
		String currentUserName = getCurrentUserName(request);
		UserDto userModel = userService.getUserByUserName(currentUserName);
		System.out.println("userId: " + userModel.getId());
		System.out.println();
		request.setAttribute("userId", userModel.getId());
		return "/Account/ChangePassword";
	}
	//修改密码
	@RequestMapping(value="/changePassword",method=RequestMethod.POST)
	@ResponseBody
	public JsonMessage changePassword(HttpServletRequest request)
	{
		JsonMessage jsonMsg = new JsonMessage();
		String userId = request.getParameter("userId");
		String oldPassword = request.getParameter("oldPassword");
		String newPassword = request.getParameter("newPassword");
		System.out.println("userId: " + userId);
		System.out.println("oldPassword: " + oldPassword);
		System.out.println("newPassword: " + newPassword);
		System.out.println();
		if (!userService.validatePassword(userId, oldPassword)) {
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("1");
			return jsonMsg;
		}
		try {
			userService.changePassword(userId, newPassword);
			jsonMsg.setSuccess(true);
			jsonMsg.setMsg("修改密码成功！");
			
		} catch (Exception e) {
			request.setAttribute("userId", userId);
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("2");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		return jsonMsg;
	}

	//返回修改密码错误页面
	@RequestMapping(value="/toChangePasswordError",method=RequestMethod.GET)
	public String toChangePasswordError(String userId, HttpServletRequest request)
	{
		System.out.println("come in toChangePassword");
		request.setAttribute("userId", userId);
		return "/Account/ChangePasswordError";
	}
	
	//返回修改密码成功页面
	@RequestMapping(value="/toChangePasswordSuccess",method=RequestMethod.GET)
	public String toChangePasswordSuccess()
	{
		return "/Account/ChangePasswordSuccess";
	}
	//返回修改密码页面
	@RequestMapping(value="/changePasswordByUserId",method=RequestMethod.GET)
	public String toChangePasswordByUserId(String userId, HttpServletRequest request)
	{
		System.out.println("come in toChangePasswordByuserId " );
		request.setAttribute("userId", userId);
		return "/Account/ChangePasswordByToken";
	}
	
	//验证用户
	@RequestMapping(value="/validateUser",method = RequestMethod.POST)
	@ResponseBody
	public String validateUser(String userName,String password, HttpServletRequest request){
//		System.out.println("userName: " + userName);
//		System.out.println("password: " +password);
		return "/index";
	}

//	//验证用户
//	@RequestMapping(value="/validateUser",method = RequestMethod.POST)
//	@ResponseBody
//	public JsonMessage validateUser(String userName,String password, HttpServletRequest request,HttpSession session)
//	{
//		JsonMessage jsonMsg = new JsonMessage();
//		try {
//			String  loginIp = IpUtil.getIpAddr(request);
//			Date loginDate = new Date();
//			Map<String, Object> validateResult =userService.validateUser(userName, password,
//					SYSTEMID, loginIp, loginDate);
//			// 如果验证通过
//			if (StringUtils.isEmpty(validateResult.get("errorMsg").toString())) {
//					UserModel userModel = (UserModel) validateResult.get("model");
//			   // 更新数据库登录状态
//			   userService.updateLoginstate(userModel.getId(), SYSTEMID, loginIp, loginDate);
//			   LoginedUserModel loginedUser =userService.CreateLoginedUser(SYSTEMID,userModel);
//			   setSessionUser(request, loginedUser);
//			   //添加日志
//			   OperationLogModel log = new OperationLogModel();
//			   {
//				   log.setSystemId(SYSTEMID);
//				   log.setUserId(userModel.getId());
//				   log.setUserName(userModel.getUserName());
//				   log.setUserIp(IpUtil.getIpAddr(request));
//				   log.setTitle("登录");
//				   log.setContent("登录系统！");
//			   }
//			   auditService.AddOperationLog(log);
//			   jsonMsg.setSuccess(true);
//			   jsonMsg.setMsg("登录成功！");
//			}
//			else {
//				jsonMsg.setSuccess(false);
//				jsonMsg.setMsg(validateResult.get("errorMsg").toString());
//			}
//
//		} catch (Exception e) {
//			jsonMsg.setSuccess(false);
//			jsonMsg.setMsg(e.getMessage());
//		}
//		return jsonMsg;		
//	}
//	
	//登出
//	@RequestMapping(value="/logOut",method=RequestMethod.GET)
//	public String logOut(HttpServletRequest request) {
//		if (getSessionUser(request)!=null) {
//			   //添加日志
//			   OperationLogModel log = new OperationLogModel();
//			   {
//				   log.setSystemId(SYSTEMID);
//				   log.setUserId(getSessionUser(request).getProfile().getId());
//				   log.setUserName(getSessionUser(request).getProfile().getUserName());
//				   log.setUserIp(IpUtil.getIpAddr(request));
//				   log.setTitle("退出");
//				   log.setContent("退出系统！");
//			   }
//			   auditService.AddOperationLog(log);
//			request.getSession().removeAttribute(CommonConstant.USER_CONTEXT);
//		}	
//		return "/Account/login";
//	}
//	

	

}
